using Coding4Fun.Phone.Controls.Helpers;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Coding4Fun.Phone.Controls
{
	[TemplateVisualState(Name = "Normal", GroupName = "GroupCommon"), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
	public class SuperSlider : Control
	{
		private const string string_0 = "BackgroundRectangle";
		private const string string_1 = "ProgressRectangle";
		private const string string_2 = "Body";
		private const string string_3 = "BarBody";
		private bool bool_0;
		private RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler_0;
		protected Rectangle BackgroundRectangle;
		protected Rectangle ProgressRectangle;
		private MovementMonitor movementMonitor_0;
		public static readonly DependencyProperty BarHeightProperty = DependencyProperty.Register("BarHeight", typeof(double), typeof(SuperSlider), new PropertyMetadata(24.0, new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty BarWidthProperty = DependencyProperty.Register("BarWidth", typeof(double), typeof(SuperSlider), new PropertyMetadata(24.0, new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(SuperSlider), new PropertyMetadata(""));
		public static readonly DependencyProperty ThumbProperty = DependencyProperty.Register("Thumb", typeof(object), typeof(SuperSlider), new PropertyMetadata(new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty BackgroundSizeProperty = DependencyProperty.Register("BackgroundSize", typeof(double), typeof(SuperSlider), new PropertyMetadata(double.NaN, new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty ProgressSizeProperty = DependencyProperty.Register("ProgressSize", typeof(double), typeof(SuperSlider), new PropertyMetadata(double.NaN, new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(SuperSlider), new PropertyMetadata(0.0, new PropertyChangedCallback(SuperSlider.OnValueChanged)));
		public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(SuperSlider), new PropertyMetadata(0.0));
		public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(SuperSlider), new PropertyMetadata(10.0));
		public static readonly DependencyProperty StepProperty = DependencyProperty.Register("Step", typeof(double), typeof(SuperSlider), new PropertyMetadata(0.0));
		public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(SuperSlider), new PropertyMetadata(1, new PropertyChangedCallback(SuperSlider.smethod_0)));
		public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Brush), typeof(SuperSlider), new PropertyMetadata(null));
		public event RoutedPropertyChangedEventHandler<double> ValueChanged
		{
			add
			{
				RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler = this.routedPropertyChangedEventHandler_0;
				RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler2;
				do
				{
					routedPropertyChangedEventHandler2 = routedPropertyChangedEventHandler;
					RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler3 = (RoutedPropertyChangedEventHandler<double>)Delegate.Combine(routedPropertyChangedEventHandler2, value);
					routedPropertyChangedEventHandler = Interlocked.CompareExchange<RoutedPropertyChangedEventHandler<double>>(ref this.routedPropertyChangedEventHandler_0, routedPropertyChangedEventHandler3, routedPropertyChangedEventHandler2);
				}
				while (routedPropertyChangedEventHandler != routedPropertyChangedEventHandler2);
			}
			remove
			{
				RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler = this.routedPropertyChangedEventHandler_0;
				RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler2;
				do
				{
					routedPropertyChangedEventHandler2 = routedPropertyChangedEventHandler;
					RoutedPropertyChangedEventHandler<double> routedPropertyChangedEventHandler3 = (RoutedPropertyChangedEventHandler<double>)Delegate.Remove(routedPropertyChangedEventHandler2, value);
					routedPropertyChangedEventHandler = Interlocked.CompareExchange<RoutedPropertyChangedEventHandler<double>>(ref this.routedPropertyChangedEventHandler_0, routedPropertyChangedEventHandler3, routedPropertyChangedEventHandler2);
				}
				while (routedPropertyChangedEventHandler != routedPropertyChangedEventHandler2);
			}
		}
		public double BarHeight
		{
			get
			{
				return (double)base.GetValue(SuperSlider.BarHeightProperty);
			}
			set
			{
				base.SetValue(SuperSlider.BarHeightProperty, value);
			}
		}
		public double BarWidth
		{
			get
			{
				return (double)base.GetValue(SuperSlider.BarWidthProperty);
			}
			set
			{
				base.SetValue(SuperSlider.BarWidthProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(SuperSlider.TitleProperty);
			}
			set
			{
				base.SetValue(SuperSlider.TitleProperty, value);
			}
		}
		public object Thumb
		{
			get
			{
				return base.GetValue(SuperSlider.ThumbProperty);
			}
			set
			{
				base.SetValue(SuperSlider.ThumbProperty, value);
			}
		}
		public double BackgroundSize
		{
			get
			{
				return (double)base.GetValue(SuperSlider.BackgroundSizeProperty);
			}
			set
			{
				base.SetValue(SuperSlider.BackgroundSizeProperty, value);
			}
		}
		public double ProgressSize
		{
			get
			{
				return (double)base.GetValue(SuperSlider.ProgressSizeProperty);
			}
			set
			{
				base.SetValue(SuperSlider.ProgressSizeProperty, value);
			}
		}
		public double Value
		{
			get
			{
				return (double)base.GetValue(SuperSlider.ValueProperty);
			}
			set
			{
				base.SetValue(SuperSlider.ValueProperty, value);
			}
		}
		public double Minimum
		{
			get
			{
				return (double)base.GetValue(SuperSlider.MinimumProperty);
			}
			set
			{
				base.SetValue(SuperSlider.MinimumProperty, value);
			}
		}
		public double Maximum
		{
			get
			{
				return (double)base.GetValue(SuperSlider.MaximumProperty);
			}
			set
			{
				base.SetValue(SuperSlider.MaximumProperty, value);
			}
		}
		public double Step
		{
			get
			{
				return (double)base.GetValue(SuperSlider.StepProperty);
			}
			set
			{
				base.SetValue(SuperSlider.StepProperty, value);
			}
		}
		public Orientation Orientation
		{
			get
			{
				return (Orientation)base.GetValue(SuperSlider.OrientationProperty);
			}
			set
			{
				base.SetValue(SuperSlider.OrientationProperty, value);
			}
		}
		public Brush Fill
		{
			get
			{
				return (Brush)base.GetValue(SuperSlider.FillProperty);
			}
			set
			{
				base.SetValue(SuperSlider.FillProperty, value);
			}
		}
		public SuperSlider()
		{
			base.set_DefaultStyleKey(typeof(SuperSlider));
			base.add_IsEnabledChanged(new DependencyPropertyChangedEventHandler(this.SuperSlider_IsEnabledChanged));
			base.add_Loaded(new RoutedEventHandler(this.SuperSlider_Loaded));
			base.add_SizeChanged(new SizeChangedEventHandler(this.SuperSlider_SizeChanged));
		}
		private void SuperSlider_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			this.method_7();
		}
		private void SuperSlider_Loaded(object sender, RoutedEventArgs e)
		{
			this.bool_0 = true;
			this.method_5();
			this.method_7();
		}
		private void SuperSlider_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			this.method_5();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.BackgroundRectangle = (base.GetTemplateChild("BackgroundRectangle") as Rectangle);
			this.ProgressRectangle = (base.GetTemplateChild("ProgressRectangle") as Rectangle);
			Grid grid = base.GetTemplateChild("Body") as Grid;
			if (grid != null)
			{
				this.movementMonitor_0 = new MovementMonitor();
				this.movementMonitor_0.Movement += new EventHandler<MovementMonitorEventArgs>(this.method_0);
				this.movementMonitor_0.MonitorControl(grid);
			}
			this.method_6();
		}
		private void method_0(object sender, MovementMonitorEventArgs e)
		{
			this.method_1(e.X, e.Y);
		}
		private static void OnValueChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			SuperSlider superSlider = dependencyObject_0 as SuperSlider;
			if (superSlider != null && dependencyPropertyChangedEventArgs_0.get_NewValue() != dependencyPropertyChangedEventArgs_0.get_OldValue())
			{
				superSlider.method_3((double)dependencyPropertyChangedEventArgs_0.get_NewValue(), (double)dependencyPropertyChangedEventArgs_0.get_OldValue());
			}
		}
		private static void smethod_0(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			SuperSlider superSlider = dependencyObject_0 as SuperSlider;
			if (superSlider != null && dependencyPropertyChangedEventArgs_0.get_NewValue() != dependencyPropertyChangedEventArgs_0.get_OldValue())
			{
				superSlider.method_5();
			}
		}
		private void method_1(double double_0, double double_1)
		{
			double num = this.method_2();
			double value = this.method_8() ? (num - double_1) : double_0;
			double num2 = ControlHelper.CheckBound(value, num);
			double num3 = this.Minimum;
			if (num != 0.0)
			{
				num3 += (this.Maximum - this.Minimum) * (num2 / num);
			}
			this.method_3(num3, this.Value);
		}
		private double method_2()
		{
			if (!this.method_8())
			{
				return base.get_ActualWidth();
			}
			return base.get_ActualHeight();
		}
		private void method_3(double double_0, double double_1)
		{
			if (!this.bool_0)
			{
				return;
			}
			this.bool_0 = true;
			if (this.Step > 0.0)
			{
				double num = double_0 % this.Step;
				double num2 = Math.Floor(double_0 - num);
				double_0 = ((num < this.Step / 2.0) ? num2 : (num2 + this.Step));
			}
			double_0 = ControlHelper.CheckBound(double_0, this.Minimum, this.Maximum);
			if (double_1 == double_0)
			{
				return;
			}
			this.Value = double_0;
			this.method_4();
			if (this.routedPropertyChangedEventHandler_0 != null)
			{
				this.routedPropertyChangedEventHandler_0.Invoke(this, new RoutedPropertyChangedEventArgs<double>(double_1, this.Value));
			}
		}
		private void method_4()
		{
			double num = this.method_2();
			double num2 = (this.Value - this.Minimum) / (this.Maximum - this.Minimum) * num;
			bool flag = this.method_8();
			SuperSlider.smethod_1(this.ProgressRectangle, flag, num2);
			FrameworkElement frameworkElement = this.Thumb as FrameworkElement;
			if (frameworkElement != null)
			{
				double num3 = flag ? frameworkElement.get_ActualHeight() : frameworkElement.get_ActualWidth();
				double num4 = ControlHelper.CheckBound(num2 - num3 / 2.0, 0.0, num - num3);
				frameworkElement.set_Margin(flag ? new Thickness(0.0, 0.0, 0.0, num4) : new Thickness(num4, 0.0, 0.0, 0.0));
			}
		}
		private void method_5()
		{
			this.method_6();
			this.method_4();
		}
		private void method_6()
		{
			if (this.ProgressRectangle != null && this.BackgroundRectangle != null)
			{
				bool flag = this.method_8();
				Grid grid = base.GetTemplateChild("BarBody") as Grid;
				if (grid != null)
				{
					if (flag)
					{
						grid.set_Width(this.BarWidth);
						grid.set_Height(double.NaN);
					}
					else
					{
						grid.set_Width(double.NaN);
						grid.set_Height(this.BarHeight);
					}
				}
				SuperSlider.smethod_2(this.ProgressRectangle, flag);
				this.ProgressRectangle.set_Width(double.NaN);
				this.ProgressRectangle.set_Height(double.NaN);
				this.BackgroundRectangle.set_Width(double.NaN);
				this.BackgroundRectangle.set_Height(double.NaN);
				if (this.ProgressSize > 0.0)
				{
					SuperSlider.smethod_1(this.ProgressRectangle, !flag, this.ProgressSize);
				}
				if (this.BackgroundSize > 0.0)
				{
					SuperSlider.smethod_1(this.BackgroundRectangle, !flag, this.BackgroundSize);
				}
				if (this.Thumb != null)
				{
					SuperSlider.smethod_2(this.Thumb as FrameworkElement, flag);
				}
				return;
			}
		}
		private void method_7()
		{
			VisualStateManager.GoToState(this, base.get_IsEnabled() ? "Normal" : "Disabled", true);
		}
		private static void smethod_1(FrameworkElement frameworkElement_0, bool bool_1, double double_0)
		{
			if (frameworkElement_0 == null)
			{
				return;
			}
			if (bool_1)
			{
				frameworkElement_0.set_Height(double_0);
				return;
			}
			frameworkElement_0.set_Width(double_0);
		}
		private bool method_8()
		{
			return this.Orientation == 0;
		}
		private static void smethod_2(FrameworkElement frameworkElement_0, bool bool_1)
		{
			if (frameworkElement_0 == null)
			{
				return;
			}
			frameworkElement_0.set_HorizontalAlignment(bool_1 ? 3 : 0);
			frameworkElement_0.set_VerticalAlignment(bool_1 ? 2 : 3);
		}
	}
}
